package com.example.taobaounion.presenter.impl

import com.example.taobaounion.model.bean.DiscountTicket
import com.example.taobaounion.net.API
import com.example.taobaounion.net.TicketPara
import com.example.taobaounion.presenter.IDiscountTicketPresenter
import com.example.taobaounion.view.IDiscountTicketCallBack
import com.example.taobaounion.utils.RetrofitManager
import com.example.taobaounion.utils.UrlUtil
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.net.HttpURLConnection

/**
 *@author : Administrator
 *@descreption : 淘口令页面的Present实现类
 */
class DiscountTicketPresenterImpl : IDiscountTicketPresenter {
    enum class State {
        LOADING, SUCEESS, NONE, ERROR
    }

    private var mCurrentState =
        State.NONE
    private var mPicUrl: String? = null
    private var data: DiscountTicket? = null
    private var mIDiscountTicketCallBack: IDiscountTicketCallBack? = null

    override fun getDiscountTicket(url: String, title: String, picUrl: String) {
        var tkUrl = url
        this.mPicUrl = picUrl
        mCurrentState =
            State.LOADING
        val retrofit = RetrofitManager.getInstance().retrofit
        val request = retrofit.create(API::class.java)

        tkUrl = UrlUtil.getHttpsUrl(url)

        val call = request.getDiscountTicket(TicketPara(tkUrl, title))
        call.enqueue(object : Callback<DiscountTicket> {
            override fun onFailure(call: Call<DiscountTicket>, t: Throwable) {
                if (mIDiscountTicketCallBack == null) {
                    mCurrentState =
                        State.ERROR
                } else {
                    mIDiscountTicketCallBack?.getDataError()
                }
            }

            override fun onResponse(call: Call<DiscountTicket>, response: Response<DiscountTicket>) {
                data = response.body()

                if (response.code() == HttpURLConnection.HTTP_OK) {
                    if (mIDiscountTicketCallBack == null) {
                        mCurrentState =
                            State.SUCEESS
                    } else {
                        if (data != null) {
                            mIDiscountTicketCallBack?.getDataSuccess(data!!, mPicUrl!!)
                        } else {
                            mIDiscountTicketCallBack?.noData()
                        }

                    }

                } else {
                    if (mIDiscountTicketCallBack == null) {
                        mCurrentState =
                            State.ERROR
                    } else {
                        mIDiscountTicketCallBack?.getDataError()
                    }
                }


            }

        })
    }

    override fun registerCallBack(callBack: IDiscountTicketCallBack) {
        this.mIDiscountTicketCallBack = callBack
        if (mCurrentState != State.NONE) {
            when (mCurrentState) {
                State.SUCEESS -> {
                    mIDiscountTicketCallBack?.getDataSuccess(data!!, mPicUrl!!)
                }
                State.ERROR -> {
                    mIDiscountTicketCallBack?.getDataError()
                }
                State.LOADING -> {
                    mIDiscountTicketCallBack?.loading()
                }
            }
        }
    }

    override fun unregisterCallBack() {
        mIDiscountTicketCallBack = null
    }
}